<?php
/**
 * News controller for News Component
 * @package Joomlart
 * @subpackage Component
 */
//Check to ensure this file is included in Joomla
defined('_JEXEC') or die( 'Restricted access' );

jimport('joomla.application.component.controller');
/**
 * News controler
 */
class JanewsControllersectionslayout extends JController
{
	/**
	 * Custom Constructor
	 */
	function __construct( $default = array())
	{
		parent::__construct( $default );

		$this->registerTask( 'apply', 'save' );
		$this->registerTask( 'preview', 'display' );
		$this->registerTask( 'edit', 'display');
		$this->registerTask( 'unpublish',	'publish' );
	}

	function display( )
	{
		switch($this->getTask())
		{
			case 'edit'    :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'layout', 'form'  );
				JRequest::setVar( 'view', 'sectionslayout'  );
			} break;

			case 'preview' :
			{
				JRequest::setVar( 'tmpl', 'component' );
				JRequest::setVar( 'view', 'sectionslayout'  );
			} break;
		}

		//Set the default view, just in case
		$view = JRequest::getCmd('view');
		if(empty($view)) {
			JRequest::setVar('view', 'sectionslayout');
			JRequest::setVar('layout', 'default');
		}

		parent::display();
	}
	
	function save()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
		
		$post = JRequest::get('post');
		$s_id = $post['cid'];
		
		$db = &JFactory::getDBO();
		$obj = new stdClass();
		$obj->secid = $s_id;
		$obj->categories = $post['cats'];
		$obj->headline = $post['headline'];
		//$obj->params = $post['']
		$obj->blog = $post['blog'];
		// Get parameter variables of category menus from the request
		$secparams		= JRequest::getVar( 'secparams', null, 'post', 'array' );
		switch ($secparams["catlayout"]) {
		  case 1:
		    $mcatparams		= JRequest::getVar( 'mcatblogparams', null, 'post', 'array' );
		    break;
		  case 2:
		    $mcatparams		= JRequest::getVar( 'mcatlistparams', null, 'post', 'array' );
		    break;
		}
		
		
		if (is_array($secparams))
		{
			$txt = array ();
			foreach ($secparams as $k => $v) {
				$txt[] = "$k=$v";
			}
      $obj->params = implode("\n", $txt);
		}
		
		if (is_array($mcatparams))
		{
			$txt = array ();
			foreach ($mcatparams as $k => $v) {
				$txt[] = "$k=$v";
			}
			$catparams = implode("\n", $txt);
		}
		
		$model = $this->getModel('sectionslayout');
		if ($post['edit']) {
			$db->updateObject('#__janews_sections', $obj, 'secid');
		}
		else {
			$db->insertObject('#__janews_sections', $obj);
			//$model->creatMenuSection($s_id,$post['sec_title']);
		}
		//update menus for all category
		$catlayout = $post['secparams']['catlayout'];
		$model->createMenuCategories($s_id, $catparams, $catlayout);
		
		switch ($this->_task)
		{
			case 'apply':
				$msg = JText::_( 'CHANGES TO SECTION LAYOUT SAVED' );
				$link = 'index.php?option=com_janews&controller=sectionslayout&task=edit&cid[]='. $s_id .'';
				break;

			case 'save':
			default:
				$msg = JText::_( 'SECTION LAYOUT SAVED' );
				$link = 'index.php?option=com_janews&controller=sectionslayout';
				break;
		}
		$this->setRedirect($link, $msg);
		
	}
	
	/**
	 * cancel editing a record
	 * @return void
	 */
	function cancel()
	{
		$msg = JText::_( 'OPERATION CANCELLED' );
		$this->setRedirect( 'index.php?option=com_janews&controller=sectionslayout', $msg );
	}
	
	function publish()
	{
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$this->setRedirect( 'index.php?option=com_janews&controller=sectionslayout' );

		// Initialize variables
		$db			=& JFactory::getDBO();
		$user		=& JFactory::getUser();
		$s_id		= JRequest::getVar( 'cid', array(), 'post', 'array' );
		$task		= JRequest::getCmd( 'task' );
		$publish	= ($task == 'publish');
		$n			= count( $s_id );

		if (empty( $s_id )) {
			return JError::raiseWarning( 500, JText::_( 'No items selected' ) );
		}

		JArrayHelper::toInteger( $s_id );
		$s_ids = implode( ',', $s_id );

		$query = 'UPDATE #__sections'
		. ' SET published = ' . (int) $publish
		. ' WHERE id IN ( '. $s_ids.'  )'
		. ' AND ( checked_out = 0 OR ( checked_out = ' .(int) $user->get('id'). ' ) )';
		$db->setQuery( $query );
		if (!$db->query()) {
			return JError::raiseWarning( 500, $row->getError() );
		}
		$this->setMessage( JText::sprintf( $publish ? 'Items published' : 'Items unpublished', $n ) );
	}

 
}
?>